package com.zwy.Shopping.servlet_;

import com.zwy.Shopping.dao.ProductDao;
import com.zwy.Shopping.dao.impl.ProductDaoImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/del_product_by_id")

public class DelProductByIdServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String productIdStr = request.getParameter("productId");
        int productId = Integer.parseInt(productIdStr);
        ProductDao productDao = new ProductDaoImpl();
      boolean isExist = productDao.isInOrderItem(productId);
      if(isExist){
          request.setAttribute("message","该商品存在于订单明细中不可删除");
          request.setAttribute("page","admin_index.jsp");
//          System.out.println((String) request.getAttribute("message"));
          request.getRequestDispatcher("alert.jsp").forward(request,response);
      }else {
        productDao.deleteProduct(productId);
      }
    }
}
